package xworker.gluon.xapp.views;

import com.gluonhq.charm.glisten.application.AppManager;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.World;
import org.xmeta.annotation.ActionField;
import xworker.dataObject.DataObject;
import xworker.gluon.glisten.application.ThingViewFactory;
import xworker.gluon.netty.ClientClient;
import xworker.gluon.xapp.XApp;

import java.util.ArrayList;
import java.util.List;

public class HomeView {
    @ActionField
    public xworker.dataObject.DataStore applicationDataStore;
    @ActionField
    public javafx.scene.control.TableView<DataObject> applicationTableView;
    @ActionField
    public xworker.dataObject.DataStore layerDataStore;
    @ActionField
    public javafx.scene.control.TableView<DataObject> layerTableView;
    @ActionField
    public xworker.dataObject.DataStore viewDataStore;
    @ActionField
    public javafx.scene.control.TableView<DataObject> viewTableView;

    public HomeView(){
        ClientClient.applicationConfigStatus.addListener((observable, oldValue, newValue) -> applicationDataStore.reload());
        ClientClient.viewConfigStatus.addListener((observable, oldValue, newValue) -> viewDataStore.reload());
        ClientClient.layerConfigStatus.addListener((observable, oldValue, newValue) -> layerDataStore.reload());
    }

    public void retrieveView(){
        DataObject data  = applicationTableView.getSelectionModel().getSelectedItem();
        if(data != null){
            AppManager appManager = AppManager.getInstance();

            String name = data.getString("name");
            appManager.retrieveView(name);
        }
    }

    public void openView(){
        DataObject data  = viewTableView.getSelectionModel().getSelectedItem();
        if(data != null){
            AppManager appManager = AppManager.getInstance();

            String name = data.getString("name");
            if("home".equals(name)){
                //默认自带的
                appManager.switchView("home");
            }else if("network".equals(name)){
                //默认自带的
                appManager.switchView(name);
            }else {
                Thing app = World.getInstance().getThing(data.getString("path"));
                 if (!appManager.isViewPresent(name)) {
                    appManager.addViewFactory(name, new ThingViewFactory(app, XApp.getApp().getActionContext()));
                }

                appManager.switchView(name);
            }
        }
    }

    public void openLayer(){
        DataObject data  = layerTableView.getSelectionModel().getSelectedItem();
        if(data != null){
            String name = data.getString("name");
            Thing app = World.getInstance().getThing(data.getString("path"));

            AppManager appManager = AppManager.getInstance();

            if(!appManager.isLayerPresent(name)){
                //appManager.addViewFactory(name, new ThingViewFactory(app, XWorkerApplication.getInstance().getActionContext()));
            }

            appManager.showLayer(name);
        }
    }

    public void openApplication(){
        DataObject data  = applicationTableView.getSelectionModel().getSelectedItem();
        if(data != null){
            //String name = data.getString("name");
            Thing app = World.getInstance().getThing(data.getString("path"));

            XApp.getApp().setAppManager(app);
        }
    }

    public static List<DataObject> loadLayers(ActionContext actionContext){
        return loadDatas("Layers@0", "xworker.gluon.xapp.views.HomeView/@Center/@mainTabPane/@applicationTab/@HBox/@applicationDataStore/@DataObjects/@AbstractDataObject");
    }

    public static List<DataObject> loadViews(ActionContext actionContext){
        return loadDatas("Views@0", "xworker.gluon.xapp.views.HomeView/@Center/@mainTabPane/@applicationTab/@HBox/@applicationDataStore/@DataObjects/@AbstractDataObject");
    }

    public static List<DataObject> loadApplication(ActionContext actionContext){
        return loadDatas("Applications@0", "xworker.gluon.xapp.views.HomeView/@Center/@mainTabPane/@applicationTab/@HBox/@applicationDataStore/@DataObjects/@AbstractDataObject");
    }

    public static List<DataObject> loadDatas(String configPath, String dataObjectPath){
        List<DataObject> datas = new ArrayList<>();
        Thing configThing = ClientClient.getConfigThing();
        Thing app = configThing.getThing(configPath);
        if(app != null){
            for(Thing child : app.getChilds()){
                DataObject data = new DataObject(dataObjectPath);
                data.put("name", child.getMetadata().getName());
                data.put("label", child.getMetadata().getLabel());
                data.put("path", child.get("path"));
                datas.add(data);
            }
        }

        return datas;
    }

}
